import { VerifyConfig } from "../contexts/VerifyContext";

// 全局验证函数引用
let globalShowVerify: ((config?: VerifyConfig) => Promise<boolean>) | null =
  null;

// 设置全局验证函数（在 VerifyProvider 中调用）
export const setGlobalVerify = (
  showVerify: (config?: VerifyConfig) => Promise<boolean>
) => {
  globalShowVerify = showVerify;
};

// 触发验证（可在任何地方调用）
export const triggerVerify = async (
  config?: VerifyConfig
): Promise<boolean> => {
  if (!globalShowVerify) {
    console.error("VerifyProvider 未初始化");
    return false;
  }
  return globalShowVerify(config);
};

// 便捷方法：图形验证码
export const showVcodeVerify = (
  title?: string,
  description?: string
): Promise<boolean> => {
  return triggerVerify({
    title: title || "安全验证",
    description: description || "请输入图片中的验证码",
  });
};

// 兼容旧版本的方法名
export const showRotateVerify = showVcodeVerify;
export const showSliderVerify = showVcodeVerify;
export const showImageVerify = showVcodeVerify;
